#include "mainwindow.h"
#include <QThreadPool>
#include <QGridLayout>
#include <QPushButton>
#include <QLabel>
#include "shelf.h"
#include "writer.h"
#include "reader.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    QGridLayout *gridLayout = new QGridLayout(this);

    gridLayout->addWidget(new QLabel("Reader Count:"), 0, 0);
    gridLayout->addWidget(new QLabel("Read Time:"), 1, 0);
    gridLayout->addWidget(new QLabel("sec"), 1, 2);
    gridLayout->addWidget(new QLabel("Writer Count:"), 2, 0);
    gridLayout->addWidget(new QLabel("Write Time:"), 3, 0);
    gridLayout->addWidget(new QLabel("sec"), 3, 2);
    gridLayout->addWidget(new QLabel("Edite Time:"), 4, 0);
    gridLayout->addWidget(new QLabel("sec"), 4, 2);
    gridLayout->addWidget(new QLabel("Book Shelf Size:"), 5, 0);

    readerCountEdt = new QLineEdit("20");
    readerCountEdt->setInputMask("999");
    gridLayout->addWidget(readerCountEdt, 0, 1, 1, 2);
    readerCount = 20;

    readTimeEdt = new QLineEdit("5");
    readTimeEdt->setInputMask("999");
    gridLayout->addWidget(readTimeEdt, 1, 1);
    readTime = 5;

    writerCountEdt = new QLineEdit("10");
    writerCountEdt->setInputMask("999");
    gridLayout->addWidget(writerCountEdt, 2, 1, 1, 2);
    writerCount = 10;

    writeTimeEdt = new QLineEdit("10");
    writeTimeEdt->setInputMask("999");
    gridLayout->addWidget(writeTimeEdt, 3, 1);
    writeTime = 10;

    editTimeEdt = new QLineEdit("6");
    editTimeEdt->setInputMask("999");
    gridLayout->addWidget(editTimeEdt, 4, 1);
    editTime = 6;

    shelfSizeEdt = new QLineEdit("50");
    shelfSizeEdt->setInputMask("999");
    gridLayout->addWidget(shelfSizeEdt, 5, 1, 1, 2);
    shelfSize = 50;

    QPushButton *startBtn = new QPushButton("Start");
    gridLayout->addWidget(startBtn, 6, 0, 1, 3);
    connect(startBtn, SIGNAL(clicked()), this, SLOT(startBtnClicked()));

    shelfBar = new QProgressBar();
    shelfBar->setMinimum(0);
    gridLayout->addWidget(shelfBar, 7, 0, 1, 3);
    shelfBar->setTextVisible(true);
    shelfBar->hide();

    activeBar = new QProgressBar();
    activeBar->setMinimum(0);
    gridLayout->addWidget(activeBar, 8, 0, 1, 3);
    activeBar->setTextVisible(true);
    activeBar->hide();

    waitBar = new QProgressBar();
    waitBar->setMinimum(0);
    gridLayout->addWidget(waitBar, 9, 0, 1, 3);
    waitBar->setTextVisible(true);
    waitBar->hide();

    setWindowFlags(Qt::MSWindowsFixedSizeDialogHint);

    QWidget *w = new QWidget(this);
    w->setLayout(gridLayout);
    setCentralWidget(w);

}

void MainWindow::shelfBarUpdate(){
    shelfBar->setValue(shelfBar->value() + 1);

}

void MainWindow::activeBarUpdate(){
    activeBar->setValue(activeBar->value() + 1);
    waitBar->setValue(waitBar->value() - 1);
}

void MainWindow::waitBarUpdate(){
    activeBar->setValue(activeBar->value() - 1);
    waitBar->setValue(waitBar->value() + 1);
}

void MainWindow::startBtnClicked(){
    readerCount = readerCountEdt->text().toInt();
    readTime = readTimeEdt->text().toInt();
    writerCount = writerCountEdt->text().toInt();
    writeTime = writeTimeEdt->text().toInt();
    editTime = editTimeEdt->text().toInt();
    shelfSize = shelfSizeEdt->text().toInt();

    shelfBar->setMaximum(shelfSize);
    activeBar->setMaximum(writerCount + readerCount);
    waitBar->setMaximum(writerCount + readerCount);

    shelfBar->show();
    activeBar->show();
    waitBar->show();

    Shelf *shelf = new Shelf(shelfSize);

    QThreadPool* threadPool = QThreadPool::globalInstance();
    threadPool->setMaxThreadCount(readerCount + writerCount);

    for(int i = 0; i < writerCount; ++i){
        Writer *writer = new Writer(QString::number(i), writeTime, editTime, shelf);
        connect(writer, SIGNAL(actived()), this, SLOT(activeBarUpdate()));
        connect(writer, SIGNAL(waited()), this, SLOT(waitBarUpdate()));
        connect(writer, SIGNAL(newBookWrited()), this, SLOT(waitBarUpdate()));
        threadPool->start(writer);
    }

    QVector<Reader*> readers;
    for(int i = 0; i < readerCount; ++i){
        Reader *reader = new Reader(readTime, shelf);
        connect(reader, SIGNAL(actived()), this, SLOT(activeBarUpdate()));
        connect(reader, SIGNAL(waited()), this, SLOT(waitBarUpdate()));
        threadPool->start(reader);
    }

}

MainWindow::~MainWindow()
{
}
